package com.cgbl.cg.adapter

import android.content.res.ColorStateList
import android.graphics.Color
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.widget.ImageViewCompat
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cgbl.cg.R
import com.cgbl.cg.utils.city.DoubleUtils
import com.robot.base.model.CouponBean
import com.robot.base.util.DrawableHelper

class CouponAdapter : BaseQuickAdapter<CouponBean, BaseViewHolder>(R.layout.app_item_coupon) {

    private var type = 1

    fun setType(type: Int) {
        this.type = type
        notifyDataSetChanged()
    }

    override fun convert(holder: BaseViewHolder, item: CouponBean) {
        val logoView = holder.getView<ImageView>(R.id.iv_logo)
        SpanUtils.with(holder.getView(R.id.tv_coupon_price))
            .append("￥")
            .setFontSize(14, true)
            .append(DoubleUtils.replace(item.couponPrice))
            .setFontSize(24, true)
            .create()
        holder.setText(R.id.tv_coupon_desc, item.couponTitle)
        holder.setText(R.id.tv_coupon_type,if(item.sageType == "0") "【通用券】" else "【指定券】")
        holder.setText(
            R.id.tv_coupon_date,
            "有效期至${TimeUtils.millis2String(item.endTime?.toLong() ?: 0, "yyyy-MM-dd")}"
        )
        if (type == TYPE_UN_USE) {
            logoView.colorFilter = null
            logoView.imageTintList = null
            holder.setVisible(R.id.iv_coupon_state, false)
            SpanUtils.with(holder.getView(R.id.tv_coupon_name))
                .append("仅限购")
                .setForegroundColor(ColorUtils.getColor(R.color.app_color_42))
                .append("《课程名称》")
                .setForegroundColor(ColorUtils.getColor(R.color.app_color_ff6060))
                .append("使用权限购")
                .setForegroundColor(ColorUtils.getColor(R.color.app_color_42))
                .create()
        }
        if (type == TYPE_USED) {
            ImageViewCompat.setImageTintList(
                logoView,
                ColorStateList.valueOf(Color.parseColor("#d0d0d0"))
            )
            holder.setTextColor(R.id.tv_coupon_name, Color.parseColor("#b5b5b5"))
            holder.setTextColor(R.id.tv_coupon_type, Color.parseColor("#b5b5b5"))
            holder.setTextColor(R.id.tv_coupon_date, Color.parseColor("#b5b5b5"))
            holder.setVisible(R.id.iv_coupon_state, true)
            holder.setImageResource(R.id.iv_coupon_state, R.mipmap.ic_coupon_used)
            holder.setText(R.id.tv_coupon_name, "仅限购《课程名称》使用权限购")
        }
        if (type == TYPE_INVALID) {
            ImageViewCompat.setImageTintList(
                logoView,
                ColorStateList.valueOf(Color.parseColor("#d0d0d0"))
            )
            holder.setTextColor(R.id.tv_coupon_name, Color.parseColor("#b5b5b5"))
            holder.setTextColor(R.id.tv_coupon_type, Color.parseColor("#b5b5b5"))
            holder.setTextColor(R.id.tv_coupon_date, Color.parseColor("#b5b5b5"))
            holder.setVisible(R.id.iv_coupon_state, true)
            holder.setImageResource(R.id.iv_coupon_state, R.mipmap.ic_coupon_invalid)
            holder.setText(R.id.tv_coupon_name, "仅限购《课程名称》使用权限购")
        }
    }

    companion object {

        const val TYPE_UN_USE = 1

        const val TYPE_USED = 2

        const val TYPE_INVALID = 3

    }
}